<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Attendance extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();

		// check if user logged in
		if(!$this->ion_auth->logged_in() && !$this->ion_auth->is_admin()){
			redirect('auth/login');
		}
		$this->load->library('form_validation');		
		$this->load->model('founder/all_attendance_model');
		$this->load->model('user/attendance_model');
		$this->load->helper('common_functions');
		$this->load->model('founder/worksheet_model');
		$this->load->model('founder/user_model');
		
	}

	public function index($id,$year_id,$month_id)
	{		
		$meta['page_title'] = 'Attendance';
		$data['daily_sheets']= $this->all_attendance_model->get_user_details($id,$year_id,$month_id);
		$data['user_id'] = $id;
		$data['attendance'] = $this->all_attendance_model->get_attendance_details($id);
		$yid = '20'.$year_id;	
		
		$data['num'] = cal_days_in_month(CAL_GREGORIAN, $month_id, $yid);
		
		//get my Salary
		$data['my_salary'] = $this->attendance_model->get_salary_by_id($id);
		
		/*getting worksheet status*/
		$data['worksheet_status'] = $this->worksheet_status($id,$month_id,$year_id);
		
		
		
		// check how many absent days		
		$search = array(
			'year_id' 	=> 	$year_id,
			'month_id'	=>	$month_id,			
			'user_id' 	=> 	$id,
		);
		
		
		/*getting data from worksheet*/
		$data['fine'] = array(		
		'fine_amount' => $this->fine($id,$month_id,$year_id,'amount'),
		'days' => $this->fine($id,$month_id,$year_id,'days'),		
		);	
		
		/*value for the widget area in attendance*/
		$data['global_data'] = array(
		'working_days' => working_days($month_id,$year_id),
		'total_working_hour' => working_days($month_id,$year_id) * $this->attendance_model->get_working_hr(),
		'min_working_hour' => $this->attendance_model->get_working_hr(),
		);	
		
		/*$data['min_working_hr'] = $this->all_attendance_model->get_working_hr($id);*/
		
		$this->load->view('common/header', $meta);
		$this->load->view('founder/attendance',$data);
		$this->load->view('common/footer');
	}
	
	public function attendance_details($year_id,$month_id){
		$meta['page_title'] = 'Attendance';
		$data['month'] = $month_id;
		
		$data['year_lst'] = $this->all_attendance_model->get_worksheet_year();
		
		//print_r($data['year_lst']);
		//exit();
		$data['attendance_details'] = $this->all_attendance_model->get_all_attendance($year_id,$month_id);		
		
		$data['check_edit_attendance'] = $this->all_attendance_model->check_edit_attendance(2,$year_id,$month_id);		
		
		/*getting ip for attendance*/
		$data['ips'] = $this->get_ip();
		
		$data['total_months'] = $this->all_attendance_model->get_attendance_months();
		//echo("<pre>");
		//print_r($data['attendance_details']);
		//exit();
		
		$this->load->view('common/header', $meta);
		$this->load->view('founder/detail-attendance',$data);
		$this->load->view('common/footer');
		
	}
	
	
	public function worksheet_timing_details($year_id,$month_id){
		$meta['page_title'] = 'Attendance';
		$data['month'] = $month_id;
		$data['year'] = $year_id;
		
		$data['users'] = $this->user_model->get_employees();
		
		$data['total_months'] = $this->all_attendance_model->get_attendance_months();
		

		$this->load->view('common/header', $meta);
		$this->load->view('founder/detail-worksheet-timings',$data);
		$this->load->view('common/footer');
		
	}
	
function worksheet_status($user_id,$month_id,$year_id){
		
		//$id = $this->session->userdata('user_id');
		$result = $this->attendance_model->worksheet_status($user_id,$month_id,$year_id);
		return $result;
	}
	
	
		/**
	* 
	* @param undefined $id
	* @param undefined $month_id
	* @param undefined $year_id
	* @param undefined $type
	* calculate the fine for each month
*/
	
	function fine($id,$month_id,$year_id,$type = NULL){
		$where  = array(
		'user_id' =>$id,
		'month_id' =>$month_id,
		'year_id' =>$year_id,
		);			
	$result = $this->attendance_model->fine($where);	
	
			if($type){
				return $result[$type];
			}else{
				return $result;
			}	
	}
	
	function save_ip(){
	$input = $this->input->post();
	$data = array(
	'from_ip' 	=> ip2long($input['from_ip']),
	'to_ip' 	=> ip2long($input['from_ip']),
	);
	$this->all_attendance_model->save_ip($data);
	}
	
	function check_ip($ip){		
		return $this->all_attendance_model->check_ip(ip2long($ip));
	}
	function get_ip(){
		return $this->all_attendance_model->get_ip();
	}
	function delete_ip(){		
		$this->all_attendance_model->delete_ip($_REQUEST['id']);
	}	
// end of function ...

}

?>